package yamen.bdwm.board;

import yamen.bdwm.BoardFragment;
import yamen.bdwm.MainActivity;
import yamen.bdwm.R;
import yamen.bdwm.compatible.ActionBarActivity;
import yamen.bdwm.data.WmMyData;
import yamen.bdwm.data.interfaces.onListRefreshedListener;
import yamen.bdwm.datastruct.WmBoardItem;
import yamen.bdwm.datastruct.WmDataAdapter;
import yamen.bdwm.datastruct.WmItem;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class BoardListFragment extends ListFragment implements
		onListRefreshedListener,GetListName {
	WmBoardItem boardItem;
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		
		View v = inflater.inflate(R.layout.list, container, false);
		Bundle bundle = getArguments();
		if (bundle != null) {
			String boardname=bundle
					.getString("board");
			
			boardItem = WmMyData.getinstance().groupmap.get(boardname);
			if (boardItem != null) {
				if (boardItem.children == null) {
					
				}
				boardItem.children.CreateAdapter(0, R.layout.thread_item);

				final WmDataAdapter adapter = boardItem.children.getAdapter(0,
						this.getActivity());

				((ListView) v.findViewById(R.id.listView1)).setAdapter(adapter);

				((ListView) v.findViewById(R.id.listView1))
						.setOnItemClickListener(new OnItemClickListener() {

							public void onItemClick(AdapterView<?> parent,
									View view, int position, long id) {

								Fragment f = ((MainActivity) BoardListFragment.this
										.getActivity()).getCurrentFragment();
								if (f instanceof BoardFragment) {

									if (((WmBoardItem) adapter
											.getItem(position)).children == null) {
										((BoardFragment) f)
												.showThreads((WmBoardItem) adapter
														.getItem(position));
									} else {
										((BoardFragment) f)
												.showBoard((WmBoardItem) ((WmItem) adapter
														.getItem(position)));
									}

								}

							}

						});
				if (adapter.getCount() == 0) {
					((ActionBarActivity) this.getActivity())
							.SetRefreshingDialog(true);
					WmMyData.getinstance().favlist.getRefreshed(this);

				}
			}
		}

		return v;

	}

	

	public void setBoard(WmBoardItem wmb) {
		Bundle bundle = new Bundle();
		if(wmb.getIndexName() != null)
			bundle.putString("board", wmb.getIndexName());
		
		this.setArguments(bundle);

	}

	public void onRefreshed(int numberOfRefreshed) {
		((ActionBarActivity) this.getActivity()).SetRefreshingDialog(false);
		if (getActivity() instanceof ActionBarActivity) {
			((ActionBarActivity) getActivity()).getActionBarHelper()
					.setRefreshActionItemState(false);
		}
		WmMyData.getinstance().favlist.getRefreshed(this);
	}

	public void onError(int i) {
		((ActionBarActivity) this.getActivity()).SetRefreshingDialog(false);
		if (getActivity() instanceof ActionBarActivity) {
			((ActionBarActivity) getActivity()).getActionBarHelper()
					.setRefreshActionItemState(false);
		}

	}



	public WmBoardItem getListItem() {
		return boardItem;
	}

}
